CentOS 7
Sponsored Link

PostgreSQL 9.5 : Install
2016/08/30
 
The version of PostgreSQL in CentOS 7 repository is 9.2 but Install 9.5 with RPM package if you need.
[1] It's possible to install from CentOS SCLo Software Collections.
It's OK to install it even if 9.2 is already installed because 9.5 is located on another PATH.
# install from SCLo

[root@dlp ~]#
yum --enablerepo=centos-sclo-rh -y install rh-postgresql95-postgresql-server
[2] Packages from Software Collections are installed uder the /opt directory.
To use it, Load environment variables like follows.
# load environment variables

[root@dlp ~]#
scl enable rh-postgresql95 bash
[root@dlp ~]#
postgres -V

postgres (PostgreSQL) 9.5.2
[root@dlp ~]#
which postgres

/opt/rh/rh-postgresql95/root/usr/bin/postgres
[3] If you'd like to enable PostgreSQL 9.5 automatically at login time, configure like follows.
[root@dlp ~]#
vi /etc/profile.d/rh-postgresql95.sh
# create new

#!/bin/bash

source /opt/rh/rh-postgresql95/enable
export X_SCLS="`scl enable rh-postgresql95 'echo $X_SCLS'`"
[4] Enable PostgreSQL 9.5 and start PostgreSQL Server.
[root@dlp ~]#
postgresql-setup --initdb --unit rh-postgresql95-postgresql

* Initializing database in '/var/opt/rh/rh-postgresql95/lib/pgsql/data'
* Initialized, logs are in /var/lib/pgsql/initdb_rh-postgresql95-postgresql.log
[root@dlp ~]#
vi /var/opt/rh/rh-postgresql95/lib/pgsql/data/postgresql.conf
# line 59: uncomment and change if allow accesses from remote hosts

listen_addresses = '
*
'
# line 421: uncomment and change if change log format

# the exmaple below is [Date User DB ***] format

log_line_prefix = '
%t %u %d
'
[root@dlp ~]#
systemctl start rh-postgresql95-postgresql

[root@dlp ~]#
systemctl enable rh-postgresql95-postgresql

[5] If Firewalld is running and also PostgreSQL is used from remote Hosts, allow service like follows.
[root@dlp ~]#
firewall-cmd --add-service=postgresql --permanent

success
[root@dlp ~]#
firewall-cmd --reload

success
[6] Set PostgreSQL admin user's password and add a user and also add a test database.
# set password

[root@dlp ~]#
su - postgres

-bash-4.2$
psql -c "alter user postgres with password 'password'"

ALTER ROLE
# add DB user "cent" as an example

-bash-4.2$
createuser cent
# create a test database (owner is the user above)

-bash-4.2$
createdb testdb -O cent
[7] Login as a user just added above and operate DataBase as test operation.
# show Databases

[cent@dlp ~]$
psql -l

                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres    +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres    +
           |          |          |             |             | postgres=CTc/postgres
 testdb    | cent     | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)

# connect to test DB

[cent@dlp ~]$
psql testdb

psql (9.5.2)
Type "help" for help.
# set password

testdb=#
alter user cent with password 'password';

ALTER ROLE
# create a test table

testdb=#
create table test ( no int,name text );

CREATE TABLE
# insert test data

testdb=#
insert into test (no,name) values (1,'cent');

INSERT 0 1
# show tables

testdb=#
select * from test;
 no | name
----+-------
  1 | cent
(1 row)
# delete test table

testdb=#
drop table test;

DROP TABLE
# quit

testdb=#
\q
# delete test database

[cent@dlp ~]$
dropdb testdb
 
Tweet